package com.kpx.app.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import com.kpx.app.Application;
import com.kpx.app.R;
import com.kpx.app.activity.ActivityCertificationPhoto;
import com.kpx.app.activity.ActivityMyPhotosNew;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.base.ComAdapter;
import com.kpx.app.base.ComViewHolder;
import com.kpx.app.bean.CertificationInfoBean;
import com.kpx.app.bean.GirlItemData;
import com.kpx.app.bean.PhotoPicBean;
import com.kpx.app.cache.FileCache;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.ImageLoader;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.uiutils.DisplayUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.wigdet.DialogFactoryImpl;

import java.util.List;

/**
 * Created by Administrator on 2017/5/15.
 */

public class PhotoAdapter extends ComAdapter {

    private int type;
    private boolean isShowEdit;

    /**
     * 构造方法
     *
     * @param context    上下文对象
     * @param datas      数据
     * @param layoutId
     * @param type
     * @param isShowEdit
     * @version 1.0
     * @createTime 2015年5月24日, 上午11:47:30
     * @updateTime 2015年5月24日, 上午11:47:30
     * @createAuthor chencong
     * @updateAuthor chencong
     * @updateInfo (此处输入修改内容, 若无修改可不写.)
     */
    public PhotoAdapter(Context context, List datas, int layoutId, int type, boolean isShowEdit) {
        super(context, datas, layoutId);
        this.type = type;
        this.isShowEdit = isShowEdit;
    }

    @Override
    public void convert(ComViewHolder holder, Object o, final int position) {
        final PhotoPicBean girlItemData = (PhotoPicBean) o;
        DisplayUtil.setViewWH_R(mContext, holder.getView(R.id.img_pic), (float) 3 / 4, 2, 6);
        holder.getView(R.id.img_isshow).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == 1) {
                    toshow(girlItemData, (position + 1));
                } else {
                    toSetShow(girlItemData);
                }
            }
        });
        holder.setOnClickListener(R.id.img_delete, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFactoryImpl.newInstance(mContext).showConfirmDialog("确定", "取消", "确定删除此照片吗？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which){
                            case DialogInterface.BUTTON_NEGATIVE:
                                delete(girlItemData);
                                break;
                            case DialogInterface.BUTTON_POSITIVE:
                                dialog.dismiss();
                                break;
                        }
                    }
                });

            }
        });
        holder.setImageURI(R.id.img_pic, girlItemData.getImgurl())
                .setVisibility(R.id.img_isshow, isShowEdit ? ComViewHolder.VISIBLE : ComViewHolder.GONE)
                .setVisibility(R.id.img_delete, isShowEdit&& type == 0? ComViewHolder.VISIBLE : ComViewHolder.GONE)
        ;
        if (type == 0) {
            holder.setImageResource_bg(R.id.img_isshow, girlItemData.getIsCoverPic() == 1 ? R.drawable.certification_photo_show : R.drawable.certification_photo_show2);
        } else {
            holder.setImageResource_bg(R.id.img_isshow, girlItemData.getIsshow() == 1 ? R.drawable.certification_photo_show : R.drawable.certification_photo_show2);
        }
    }

    private void delete(final PhotoPicBean girlItemData) {
        ((ActivityBase) mContext).showLoadingDialog();
        MyHashMap map = new MyHashMap();
        map.put("dmId", girlItemData.getDmId() + "");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                ((ActivityBase) mContext).dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    mDatas.remove(girlItemData);
                    notifyDataSetChanged();
                    ToastUtil.getInstance().showToast(mContext, "删除成功");
                } else {
                    ToastUtil.getInstance().showToast(mContext, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                ((ActivityBase) mContext).dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(mContext, error);
            }

        }, null, null).POST(HttpDataReqUrl.URL_POST_GET_DELETE_PIC, true, map);
    }

    private void toshow(final PhotoPicBean girlItemData, final int position) {
        ((ActivityBase) mContext).showLoadingDialog();
        MyHashMap map = new MyHashMap();
        map.put("number", position + "");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                ((ActivityBase) mContext).dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    for (int i = 0; i < mDatas.size(); i++) {
                        ((PhotoPicBean) mDatas.get(i)).setIsshow(2);
                    }
                    girlItemData.setIsshow(1);
                    notifyDataSetChanged();
                } else {
                    ToastUtil.getInstance().showToast(mContext, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                ((ActivityBase) mContext).dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(mContext, error);
            }

        }, null, null).POST(HttpDataReqUrl.URL_POST_SET_SHOW_PIC, true, map);
    }

    private void toSetShow(final PhotoPicBean girlItemData) {
        ((ActivityBase) mContext).showLoadingDialog();
        MyHashMap map = new MyHashMap();
        map.put("dmId", girlItemData.getDmId() + "");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                ((ActivityBase) mContext).dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    for (int i = 0; i < mDatas.size(); i++) {
                        ((PhotoPicBean) mDatas.get(i)).setIsCoverPic(2);
                    }
                    girlItemData.setIsCoverPic(1);
                    notifyDataSetChanged();
                } else {
                    ToastUtil.getInstance().showToast(mContext, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                ((ActivityBase) mContext).dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(mContext, error);
            }

        }, null, null).POST(HttpDataReqUrl.URL_POST_PHOTO_COVER, true, map);
    }

    public void setIsShowEdit(boolean isShowEdit) {
        this.isShowEdit = isShowEdit;
        notifyDataSetChanged();
    }
}
